package cn.zjh.org.service.impl;

import cn.zjh.basic.service.impl.BaseServiceImpl;
import cn.zjh.org.domain.Department;
import cn.zjh.org.mapper.DepartmentMapper;
import cn.zjh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> peatTree() {
        //将遍历出的部门都装到map集合当中
        List<Department> mapperAll = departmentMapper.findAll();
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department:mapperAll){
            map.put(department.getId(),department);
        }

        List<Department> peatTree = new ArrayList<>();

        for(Department department : mapperAll){
                if (department.getParent_id() == null){//获取所有的顶级部门
                    peatTree.add(department);
                }else {
                    Long parent_id = department.getParent_id();

                    Department parentDepartment = map.get(parent_id);

                    parentDepartment.getChildren().add(department);
                }
        }

        return peatTree;
    }

}
